package com.lagou.edu.service.impl;

import com.lagou.edu.service.MailService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

/**
 * @author lyj
 * @Title: MailServiceImpl
 * @ProjectName homework-parent
 * @Description: TODO
 * @date 2021/1/2 22:20
 */
@Service
public class MailServiceImpl implements MailService {


    @Value("${Spring.mail.fromAddr}")
    private String from;

    @Value("${Spring.mail.nickName}")
    private String nickName;
    @Override
    public void sendSimpleEmail(String to, String subject, String content) {
        JavaMailSender javaMailSender=new JavaMailSenderImpl();
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(nickName + "<" + from + ">");
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);

        try{
            javaMailSender.send(simpleMailMessage);
            System.out.println("简易邮件发送成功");
        } catch(Exception e) {
            System.out.println("简易邮件发送异常");
            e.printStackTrace();
        }
    }
}
